package com.huixh.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huixh.train.common.exception.BusinessException;
import com.huixh.train.common.exception.BusinessExceptionEnum;
import com.huixh.train.common.resp.PageResult;
import com.huixh.train.business.domain.Station;
import com.huixh.train.business.domain.StationExample;
import com.huixh.train.business.mapper.StationMapper;
import com.huixh.train.business.req.StationQueryReq;
import com.huixh.train.business.req.StationSaveReq;
import com.huixh.train.business.resp.StationQueryResp;
import com.huixh.train.business.service.StationService;
import com.huixh.train.common.resp.Result;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huixh
 */
@Service
public class StationServiceImpl implements StationService {

    private final Logger logger = LoggerFactory.getLogger(StationServiceImpl.class);
    @Resource
    private StationMapper stationMapper;

    @Resource
    private SnowUtil snowUtil;

    @Override
    public void save(StationSaveReq stationSaveReq) {
        DateTime now = DateTime.now();
        Station station = BeanUtil.copyProperties(stationSaveReq, Station.class);

        Station unique = selectByUnique(station.getName());
        if (ObjectUtil.isNotNull(unique)){
            throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_EXISTS);
        }
        if (ObjectUtil.isNull(station.getId())) {
            station.setId(snowUtil.getSnowflakeToLong());
            station.setCreateTime(now);
            station.setUpdateTime(now);
            stationMapper.insert(station);
        } else {
            station.setUpdateTime(now);
            stationMapper.updateByPrimaryKey(station);
        }
    }

    private Station selectByUnique(String name) {
        StationExample stationExample = new StationExample();
        stationExample.createCriteria().andNameEqualTo(name);

        List<Station> stationList = stationMapper.selectByExample(stationExample);

        if (CollUtil.isEmpty(stationList)) {
            return null;
        } else {
            return stationList.get(0);
        }

    }

    @Override
    public PageResult<List<StationQueryResp>> queryList(StationQueryReq stationQueryReq) {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("id desc");

        logger.info("查询页码：{}", stationQueryReq.getPageNum());
        logger.info("每页条数：{}", stationQueryReq.getPageSize());
        PageHelper.startPage(stationQueryReq.getPageNum(), stationQueryReq.getPageSize());
        List<Station> stationList = stationMapper.selectByExample(stationExample);

        PageInfo<Station> pageInfo = new PageInfo<>(stationList);
        logger.info("总行数：{}", pageInfo.getTotal());
        logger.info("总页数：{}", pageInfo.getPages());

        List<StationQueryResp> list = BeanUtil.copyToList(stationList, StationQueryResp.class);
        return PageResult.success(pageInfo.getTotal(), list);
    }

    @Override
    public void delete(Long id) {
        stationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Result<List<StationQueryResp>> getAllList() {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");

        List<Station> stationList = stationMapper.selectByExample(stationExample);

        List<StationQueryResp> queryResps = BeanUtil.copyToList(stationList, StationQueryResp.class);

        return Result.success(queryResps);
    }
}